/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package bgu.ppm.dat.view;

import bgu.ppm.dat.model.Pivot;
import bgu.ppm.dat.model.Table;
import bgu.sonar.util.ui.visual.HasVisualData;
import java.awt.BorderLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.List;
import javax.swing.AbstractButton;
import javax.swing.ButtonGroup;
import javax.swing.ImageIcon;
import javax.swing.JComponent;
import javax.swing.JToggleButton;
import org.apache.commons.collections15.EnumerationUtils;

/**
 *
 * @author User
 */
public class PivotTableWorkbunchView extends javax.swing.JPanel implements Pivot.PivotListener {

    private List<TableVisualizationFactory> tableViewerFactories;
    private Pivot model;
    private TableVisualizationFactory currentFactory;

    /**
     * Creates new form PivotTableWorkbunchView
     */
    public PivotTableWorkbunchView() {
        initComponents();
    }

    public void setTableViewerFactories(List<TableVisualizationFactory> tableViewerFactories) {
        this.tableViewerFactories = tableViewerFactories;

        visualizationBar.removeAll();
        visualizationGroup = new ButtonGroup();

        for (final TableVisualizationFactory factory : tableViewerFactories) {
            final JToggleButton b = new JToggleButton(factory.getVisualName());
            if (factory.getIcon() != null) {
                b.setIcon(new ImageIcon(factory.getIcon()));
            }

            b.getModel().addActionListener(new ActionListener() {
                @Override
                public void actionPerformed(ActionEvent e) {
                    visualizationContainer.removeAll();
                    visualizationContainer.add(
                            factory.getComponent(model.getPivotTable()), BorderLayout.CENTER);
                    invalidate();
                    revalidate();
                    repaint();
                }
            });

            visualizationBar.add(b);
            visualizationGroup.add(b);

            if (visualizationGroup.getButtonCount() == 1) {
                visualizationGroup.setSelected(b.getModel(), true);
            }
        }
    }

    public Pivot getModel() {
        return model;
    }

    public void setModel(Pivot model) {
        if (this.model != null) {
            this.model.getListeners().remove(this);
        }

        model.getListeners().add(this);

        this.model = model;

        pivotView.setModel(model);

        updateTableVisualizationView();
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        visualizationGroup = new javax.swing.ButtonGroup();
        jSplitPane2 = new javax.swing.JSplitPane();
        jPanel = new javax.swing.JPanel();
        visualizationBar = new javax.swing.JToolBar();
        visualizationContainer = new javax.swing.JPanel();
        pivotView = new bgu.ppm.dat.view.PivotView();

        setLayout(new java.awt.BorderLayout());

        jSplitPane2.setDividerLocation(400);

        jPanel.setLayout(new java.awt.BorderLayout());

        visualizationBar.setRollover(true);
        jPanel.add(visualizationBar, java.awt.BorderLayout.NORTH);

        visualizationContainer.setLayout(new java.awt.BorderLayout());
        jPanel.add(visualizationContainer, java.awt.BorderLayout.CENTER);

        jSplitPane2.setLeftComponent(jPanel);

        pivotView.setPreferredSize(new java.awt.Dimension(250, 730));
        jSplitPane2.setRightComponent(pivotView);

        add(jSplitPane2, java.awt.BorderLayout.CENTER);
    }// </editor-fold>//GEN-END:initComponents
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JPanel jPanel;
    private javax.swing.JSplitPane jSplitPane2;
    private bgu.ppm.dat.view.PivotView pivotView;
    private javax.swing.JToolBar visualizationBar;
    private javax.swing.JPanel visualizationContainer;
    private javax.swing.ButtonGroup visualizationGroup;
    // End of variables declaration//GEN-END:variables

    @Override
    public void pivotChanged(Pivot pivot) {
        updateTableVisualizationView();
    }

    private void updateTableVisualizationView() {
        for (AbstractButton b : EnumerationUtils.toList(visualizationGroup.getElements())) {
            if (b.isSelected()) {
                b.doClick();
            }
        }
    }

    public static interface TableVisualizationFactory extends HasVisualData {

        JComponent getComponent(Table table);
    }
}